package com.cs.disposition.mqtt;


import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqttConfig {

    private static final String MQTT_BROKER_URL = "tcp://broker.emqx.io:1883"; // 替换为你的MQTT Broker地址
    private static final String CLIENT_ID = "spring-boot-client";

    @Bean
    public MqttClient mqttClient(MqttDefaultCallbackService mqttCallbackService) throws MqttException {
        MqttClient mqttClient = new MqttClient(MQTT_BROKER_URL, CLIENT_ID);
        mqttClient.setCallback(mqttCallbackService);

        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true); // 设置为干净会话
        mqttClient.connect(options);
        return mqttClient;
    }
}
